package com.xinyou.u8c.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinyou.u8c.entity.*;
import com.xinyou.u8c.mapper.GlAccvouchMapper;
import com.xinyou.u8c.result.JsonResult;
import com.xinyou.u8c.service.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chq
 * @since 2023-08-23
 */
@Slf4j
@RestController
@RequestMapping("/u8c/gl-accvouch")
public class GlAccvouchController {

    @Autowired
    private IGlAccvouchService glAccvouchService;

    @Autowired
    private IFiglaccountingdocumentzjbService figlaccountingdocumentzjbService;

    @Autowired
    private IFiglaccdoccodezjbService figlaccdoccodezjbService;

    @Autowired
    private IFiglaccdocentryzjbService figlaccdocentryzjbService;

    @Autowired
    private IDsignService dsignService;

    @Autowired
    private IMDMAccountOrgService imdmAccountOrgService;

    @Autowired
    private IMDMAccountPeriodService imdmAccountPeriodService;

    @Autowired
    private IFiglaccdocassistancezjbService figlaccdocassistancezjbService;

    @Autowired
    private GlAccvouchMapper glAccvouchMapper;

    @Autowired
    private IAssociatedCertificateService associatedCertificateService;

    @Autowired
    private IMDMSubjectService imdmSubjectService;

    @Autowired
    private ISettleStyleService settleStyleService;

    @Autowired
    private IMDMCurrencyService imdmCurrencyService;

    @Autowired
    private IMDMLedgerService imdmLedgerService;


    @Autowired
    private IDepartmentService departmentService;

    @Autowired
    private IMDMOrganizationService imdmOrganizationService;

    @Autowired
    private IPersonGuidService personGuidService;

    @Autowired
    private IPersonService personService;

    @Autowired
    private IBfmasterorganizationzjbService bfmasterorganizationzjbService;

    @Autowired
    private IBfadminorganizationzjbService bfadminorganizationzjbService;

    @Autowired
    private IBfaccountingdepartmentzjbService bfaccountingdepartmentzjbService;

    @Autowired
    private IBfpartnerzjbService bfpartnerzjbService;

    @Autowired
    private ICustomerService customerService;

    @Autowired
    private IMDMAdminOrgService imdmAdminOrgService;

    @Autowired
    private IMDMPartnerTypeService imdmPartnerTypeService;

    @Autowired
    private IVendorService iVendorService;

    @Autowired
    private IBfemployeezjbService iBfemployeezjbService;

    @Autowired
    private IBfaccountingemployeezjbService iBfaccountingemployeezjbService;

    @Autowired
    private IBfcustomitemzjbService iBfcustomitemzjbService;

    @Autowired
    private IMDMAccountProTypeService imdmAccountProTypeService;

    @Autowired
    private IFiglaccdoctypezjbService iFiglaccdoctypezjbService;

    @Autowired
    private IUserDefineService iUserDefineService;

    @Autowired
    private IFitemss98Service iFitemss98Service;

    @Autowired
    private IBankService iBankService;

    @Autowired
    private IFitemss03Service iFitemss03Service;

    @Autowired
    private IFitemssZFService iFitemssZFService;

    @Autowired
    private IFitemss02Service iFitemss02Service;

    @Autowired
    private IFitemss00Service iFitemss00Service;

    @Autowired
    private IInventoryService iInventoryService;

    @Autowired
    private IMDMCodeItemsService imdmCodeItemsService;

    @Autowired
    private ICustomerBankService iCustomerBankService;

    @Autowired
    private IMDMPartnerAreaService imdmPartnerAreaService;

    @Autowired
    private ITransPartnerService iTransPartnerService;

    @Autowired
    private IMDMPartnerService imdmPartnerService;

    public final MDMCurrency getMdmCurrency() {
        return imdmCurrencyService.getOne(new LambdaQueryWrapper<MDMCurrency>().eq(MDMCurrency::getNameChs, "人民币"));
    }

    public final MDMAccountOrg getMdmAccountOrg() {
        return imdmAccountOrgService.getOne(new LambdaQueryWrapper<MDMAccountOrg>().eq(MDMAccountOrg::getNameChs, "贵州永红航空机械有限责任公司"));
    }

    public final MDMOrganization getMdmOrganization() {
        return imdmOrganizationService.getOne(new LambdaQueryWrapper<MDMOrganization>().eq(MDMOrganization::getNameChs, "贵州永红航空机械有限责任公司"));
    }

    public final MDMLedger getMdmLedger() {
//        根据核算组织Id查询账薄Id，parentid暂时没有关联核算组织id
        return imdmLedgerService.getOne(new LambdaQueryWrapper<MDMLedger>().eq(MDMLedger::getParentid, getMdmAccountOrg().getId()));
    }

    public PersonGuid getPersonGuid(String cDepPerson) {
        return personGuidService.getOne(new LambdaQueryWrapper<PersonGuid>().eq(PersonGuid::getCPersonCode, cDepPerson));
    }

    public Department getDepartment(String cDepName, Integer iDepGrade) {
        return departmentService.getOne(new LambdaQueryWrapper<Department>().eq(Department::getCDepName, cDepName.split("/")[iDepGrade - 2]));
    }

    public Department getDepartment(String cDepCode) {
        return departmentService.getOne(new LambdaQueryWrapper<Department>().eq(Department::getCDepCode, cDepCode));
    }

    public final MDMAdminOrg getMdmAdmin() {
        return imdmAdminOrgService.getOne(new LambdaQueryWrapper<MDMAdminOrg>().eq(MDMAdminOrg::getNameChs, "贵州永红航空机械有限责任公司"));
    }

    public final MDMPartnerType getMDMPartnerType() {
        return imdmPartnerTypeService.getOne(new LambdaQueryWrapper<MDMPartnerType>().eq(MDMPartnerType::getNameChs, "集团外"));
    }

    public final MDMCodeItems getMDMCodeItems() {
        return imdmCodeItemsService.getOne(new LambdaQueryWrapper<MDMCodeItems>().like(MDMCodeItems::getNameChs, "个体工商户"));
    }

    /**
     * 将所有的i_id插入主键关联凭证的表中
     *
     * @return
     */
    public boolean selectAllKey() {
        List<AssociatedCertificate> associatedCertificates = glAccvouchMapper.selectAllKey();
        List<AssociatedCertificate> associatedCertificateOnes = glAccvouchMapper.selectAllKeyOne();
        for (AssociatedCertificate associatedCertificateOne : associatedCertificateOnes) {
            for (AssociatedCertificate associatedCertificate : associatedCertificates) {
                if (Objects.equals(associatedCertificateOne.getInoId(), associatedCertificate.getInoId()) && associatedCertificateOne.getIId().equals(associatedCertificate.getIId())) {
                    if (associatedCertificateService.getOne(new LambdaQueryWrapper<AssociatedCertificate>().eq(AssociatedCertificate::getIId, associatedCertificate.getIId())) == null) {
                        associatedCertificate.setId(UUID.randomUUID().toString());
                        associatedCertificate.setDbillDate(associatedCertificateOne.getDbillDate());
                        associatedCertificate.setCdigest(associatedCertificateOne.getCdigest());
                        associatedCertificate.setCbill(associatedCertificateOne.getCbill());
                        associatedCertificate.setCcheck(associatedCertificateOne.getCcheck());
                        associatedCertificate.setDbillDate(associatedCertificateOne.getDbillDate());
                        associatedCertificate.setIsignseq(associatedCertificateOne.getIsignseq());
                        associatedCertificateService.save(associatedCertificate);
                    }
                }
            }
        }
        return associatedCertificateService.count() > 0;
    }

    @GetMapping("/getPersonGuid")
    public JsonResult<String> getPersonGuid() {
        List<Person> list = personService.list();
        PersonGuid personGuid = new PersonGuid();
        for (Person person : list) {
            if (personGuidService.getOne(new LambdaQueryWrapper<PersonGuid>().eq(PersonGuid::getCPersonCode, person.getCPersonCode())) == null) {
                personGuid.setCPersonCode(person.getCPersonCode());
                personGuid.setId(UUID.randomUUID().toString());
                personGuidService.save(personGuid);
            }
        }
        return new JsonResult<>(true, "成功");
    }

    @GetMapping("/getPrivateData")
    public JsonResult<String> getPrivateData() {
        getPersonGuid();
        if (selectAllKey()) {
            if (getOrganizationzjb()) {
                getBfadminorganizationzjb();
                getBfaccountingdepartmentzjb();
            }
            getPartner();
            if (getBfemployeezjb()) {
                getBfaccountingemployeezjb();
            }
            getFiglaccdoctypezjb();
            getBfcustomitemzjb();
        }
        return new JsonResult<>(true, "成功");
    }

    /**
     * 组织机构部门
     */
    public Boolean getOrganizationzjb() {
        Bfmasterorganizationzjb bfmasterorganizationzjb = new Bfmasterorganizationzjb();
        bfmasterorganizationzjb.setCurrency(getMdmCurrency().getId());
        bfmasterorganizationzjb.setOwnerid(getMdmOrganization().getId());
        bfmasterorganizationzjb.setAccountingorg("0");
        bfmasterorganizationzjb.setAdminorg("1");
        bfmasterorganizationzjb.setOrgtype("33c21504-3384-42a7-8fc3-c1b5e9e982d0");
        bfmasterorganizationzjb.setSecuritylevel("BFPUBLIC");
//      特别注意！！！该行财务主数据编码规则不确定
        List<Department> list = departmentService.list();
        for (Department department : list) {
            if (bfmasterorganizationzjbService.getById(department.getCDepGUID()) == null) {
                bfmasterorganizationzjb.setId(department.getCDepGUID());
                if (department.getCDepPerson() != null) {
                    bfmasterorganizationzjb.setHead(getPersonGuid(department.getCDepPerson()).getId());
                }
                if (department.getIDepGrade() != 1) {
                    bfmasterorganizationzjb.setPnthrinfoParentelement(getDepartment(department.getCDepFullName(), department.getIDepGrade()).getCDepGUID());
                } else {
                    bfmasterorganizationzjb.setPnthrinfoParentelement(getMdmOrganization().getId());
                }
                if (department.getCDepLeader() != null) {
                    bfmasterorganizationzjb.setIncharge(getPersonGuid(department.getCDepLeader()).getId());
                }
                if (department.getDDepEndDate() != null) {
                    bfmasterorganizationzjb.setStateIsenabled("0");
                } else {
                    bfmasterorganizationzjb.setStateIsenabled("1");
                }
                bfmasterorganizationzjb.setPnthrinfoSequence(department.getIDepOrder().toString());
//                特别注意！！！该处与以财务主数据编码规则为准
                String treeInfoPath = getTreeInfoPath(department);
                bfmasterorganizationzjb.setCode(treeInfoPath.substring(2, treeInfoPath.length()));
                bfmasterorganizationzjb.setNameChs(department.getCDepName());
                if (department.getBDepEnd().toString().equals("true")) {
                    bfmasterorganizationzjb.setPnthrinfoIsdetail("1");
                    bfmasterorganizationzjb.setTreeinfoIsdetail("1");
                } else {
                    bfmasterorganizationzjb.setPnthrinfoIsdetail("0");
                    bfmasterorganizationzjb.setTreeinfoIsdetail("0");
                }
                bfmasterorganizationzjb.setPnthrinfoLayer(String.valueOf(department.getIDepGrade()+1));
                bfmasterorganizationzjb.setTreeinfoLayer(String.valueOf(department.getIDepGrade()+1));
                bfmasterorganizationzjb.setTreeinfoPath(getMdmOrganization().getPathcodePath()+getTreeInfoPath(department));
                bfmasterorganizationzjb.setFullpathnameChs("贵州永红航空机械有限责任公司-" + department.getCDepFullName().replace("/", "-"));
                bfmasterorganizationzjbService.save(bfmasterorganizationzjb);
            }
        }
        return true;
    }

    private void reportVendor(Vendor vendor) {
        Bfpartnerzjb bfpartnerzjb = new Bfpartnerzjb();
        bfpartnerzjb.setDomaintype("1");
        bfpartnerzjb.setInteriorcompany("0");
        bfpartnerzjb.setIsother("0");
        bfpartnerzjb.setSeclevel("0");
        bfpartnerzjb.setSeclevelid("BFPUBLIC");
        bfpartnerzjb.setOwnerorg(getMdmAdmin().getId());
        bfpartnerzjb.setUnitnature(getMDMCodeItems().getId());
        bfpartnerzjb.setType(getMDMPartnerType().getId());
        if (bfpartnerzjbService.getOne(new LambdaQueryWrapper<Bfpartnerzjb>().eq(Bfpartnerzjb::getNameChs, vendor.getCVenName())) == null) {
            bfpartnerzjb.setId(UUID.randomUUID().toString());
            if (vendor.getCDCCode() != null) {
                List<MDMPartnerArea> list1 = imdmPartnerAreaService.list(new LambdaQueryWrapper<MDMPartnerArea>().eq(MDMPartnerArea::getCode, vendor.getCDCCode()));
                if(list1.size()!=0){
                    bfpartnerzjb.setArea(list1.get(0).getId());
                }
            }
            bfpartnerzjb.setCode(vendor.getCVenCode());
//                特别注意！！！社会信用代码暂时设置为必填，需要讨论出具体的解决方案出来
            if (vendor.getCVenRegCode() != null) {
                bfpartnerzjb.setOrganizationcode(vendor.getCVenRegCode());
                bfpartnerzjb.setCompanyorperson("0");
            } else {
                bfpartnerzjb.setCompanyorperson("1");
            }
            bfpartnerzjb.setIsvender("0");
            bfpartnerzjb.setIscustomer("1");
            if (vendor.getDEndDate() != null) {
                bfpartnerzjb.setStateIsenabled("0");
            } else {
                bfpartnerzjb.setStateIsenabled("1");
            }
            bfpartnerzjb.setNameChs(vendor.getCVenName());
            bfpartnerzjbService.save(bfpartnerzjb);
        }

    }

    public void reportCustomer(Customer customer){
        Bfpartnerzjb bfpartnerzjb = new Bfpartnerzjb();
        bfpartnerzjb.setDomaintype("1");
        bfpartnerzjb.setInteriorcompany("0");
        bfpartnerzjb.setIsother("0");
        bfpartnerzjb.setSeclevel("0");
        bfpartnerzjb.setSeclevelid("BFPUBLIC");
        bfpartnerzjb.setOwnerorg(getMdmAdmin().getId());
        bfpartnerzjb.setUnitnature(getMDMCodeItems().getId());
        bfpartnerzjb.setType(getMDMPartnerType().getId());
        if (bfpartnerzjbService.getOne(new LambdaQueryWrapper<Bfpartnerzjb>().eq(Bfpartnerzjb::getNameChs, customer.getCCusName())) == null) {
            bfpartnerzjb.setId(UUID.randomUUID().toString());
            if (customer.getCDCCode() != null) {
                List<MDMPartnerArea> list1 = imdmPartnerAreaService.list(new LambdaQueryWrapper<MDMPartnerArea>().eq(MDMPartnerArea::getCode, customer.getCDCCode()));
                if(list1.size()!=0){
                    bfpartnerzjb.setArea(list1.get(0).getId());
                }
            }
            bfpartnerzjb.setCode(customer.getCCusCode());
//                特别注意！！！社会信用代码暂时设置为必填，需要讨论出具体的解决方案出来
            if (customer.getCCusRegCode() != null) {
                bfpartnerzjb.setOrganizationcode(customer.getCCusRegCode());
                bfpartnerzjb.setCompanyorperson("0");
            } else {
                bfpartnerzjb.setCompanyorperson("1");
            }
            bfpartnerzjb.setIsvender("0");
            bfpartnerzjb.setIscustomer("1");
            if (customer.getDEndDate() != null) {
                bfpartnerzjb.setStateIsenabled("0");
            } else {
                bfpartnerzjb.setStateIsenabled("1");
            }
            bfpartnerzjb.setNameChs(customer.getCCusName());
            bfpartnerzjbService.save(bfpartnerzjb);
        }
    }
    /**
     * 行政部门
     */
    public void getBfadminorganizationzjb() {
        Bfadminorganizationzjb bfadminorganizationzjb = new Bfadminorganizationzjb();
        bfadminorganizationzjb.setOwnerid(getMdmOrganization().getId());
        bfadminorganizationzjb.setSecuritylevel("BFPUBLIC");
        bfadminorganizationzjb.setOrgtype("33c21504-3384-42a7-8fc3-c1b5e9e982d0");
        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.in(Department::getCDepName, "综合管理部、规划运营部、改革办公室、人力资源部、财务部、党群工作部、纪检审计部、市场发展部".split("、"));
        List<Department> list = departmentService.list(departmentLambdaQueryWrapper);
        for (Department department : list) {
            if (bfadminorganizationzjbService.getById(department.getCDepGUID()) == null) {
                bfadminorganizationzjb.setId(department.getCDepGUID());
                bfadminorganizationzjb.setMasterorgid(department.getCDepGUID());
                if (department.getCDepPerson() != null) {
                    bfadminorganizationzjb.setDepartmenthead(getPersonGuid(department.getCDepPerson()).getId());
                }
                if (department.getIDepGrade() != 1) {
                    bfadminorganizationzjb.setPnthrinfoParentelement(getDepartment(department.getCDepFullName(), department.getIDepGrade()).getCDepGUID());
                } else {
                    bfadminorganizationzjb.setPnthrinfoParentelement(getMdmOrganization().getId());
                }
                if (department.getCDepLeader() != null) {
                    bfadminorganizationzjb.setIncharge(getPersonGuid(department.getCDepLeader()).getId());
                }
                if (department.getDDepEndDate() != null) {
                    bfadminorganizationzjb.setStateIsenabled("0");
                } else {
                    bfadminorganizationzjb.setStateIsenabled("1");
                }
                bfadminorganizationzjb.setPnthrinfoSequence(department.getIDepOrder().toString());
                String treeInfoPath = getTreeInfoPath(department);
                bfadminorganizationzjb.setCode(treeInfoPath.substring(2, treeInfoPath.length()));
                bfadminorganizationzjb.setNameChs(department.getCDepName());
                if (department.getBDepEnd().toString().equals("true")) {
                    bfadminorganizationzjb.setPnthrinfoIsdetail("1");
                    bfadminorganizationzjb.setTreeinfoIsdetail("1");
                } else {
                    bfadminorganizationzjb.setPnthrinfoIsdetail("0");
                    bfadminorganizationzjb.setTreeinfoIsdetail("0");
                }
                bfadminorganizationzjb.setPnthrinfoLayer(String.valueOf(department.getIDepGrade()+1));
                bfadminorganizationzjb.setTreeinfoLayer(String.valueOf(department.getIDepGrade()+1));
                bfadminorganizationzjb.setTreeinfoPath(getMdmOrganization().getPathcodePath()+getTreeInfoPath(department));
                bfadminorganizationzjb.setFullpathnameChs("贵州永红航空机械有限责任公司" + department.getCDepFullName().replace("/", ""));
                bfadminorganizationzjbService.save(bfadminorganizationzjb);
            }
        }
    }

    /**
     * 核算部门
     */
    public void getBfaccountingdepartmentzjb() {
        Bfaccountingdepartmentzjb bfaccountingdepartmentzjb = new Bfaccountingdepartmentzjb();
        bfaccountingdepartmentzjb.setAccorgid(getMdmAccountOrg().getId());
        bfaccountingdepartmentzjb.setFullnamerole("0");
        bfaccountingdepartmentzjb.setSecuritylevel("FI_PUBLIC");
        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.eq(Department::getCDepName, "财务部");
        Department department = departmentService.getOne(departmentLambdaQueryWrapper);
        System.out.println(department.getCDepGUID());
        if (bfaccountingdepartmentzjbService.getById(department.getCDepGUID()) == null) {
//            特别警告！！！id是否与行政组织机构id保持一致
            bfaccountingdepartmentzjb.setId(department.getCDepGUID());
            bfaccountingdepartmentzjb.setAdmindeptid(department.getCDepGUID());
            if (department.getDDepEndDate() != null) {
                bfaccountingdepartmentzjb.setDisableyear(department.getDDepEndDate().substring(0, 4));
            }
            if (department.getDDepEndDate() != null) {
                bfaccountingdepartmentzjb.setIsdisable("0");
            } else {
                bfaccountingdepartmentzjb.setIsdisable("1");
            }
            bfaccountingdepartmentzjb.setSortorder(department.getIDepOrder().toString());
//                特别注意！！！该处与以财务主数据编码规则为准B01
            String treeInfoPath = getTreeInfoPath(department);
            bfaccountingdepartmentzjb.setCode(treeInfoPath.substring(2, treeInfoPath.length()));
            bfaccountingdepartmentzjb.setNameChs(department.getCDepName());
            if (department.getBDepEnd().toString().equals("true")) {
                bfaccountingdepartmentzjb.setTreeinfoIsdetail("1");
            } else {
                bfaccountingdepartmentzjb.setTreeinfoIsdetail("0");
            }
            bfaccountingdepartmentzjb.setTreeinfoLayer(String.valueOf(department.getIDepGrade()));
            bfaccountingdepartmentzjb.setTreeinfoPath(getTreeInfoPath(department));
            if (department.getIDepGrade() != 1) {
                bfaccountingdepartmentzjb.setParentid(getDepartment(department.getCDepFullName(), department.getIDepGrade()).getCDepGUID());
            } else {
                bfaccountingdepartmentzjb.setParentid(getMdmOrganization().getId());
            }
            bfaccountingdepartmentzjb.setFullnameChs(department.getCDepFullName().replace("/", "-"));
            bfaccountingdepartmentzjbService.save(bfaccountingdepartmentzjb);
        }
    }

    @GetMapping("/getPartner")
    public void getPartner() {
        List<Customer> list = customerService.list(new LambdaQueryWrapper<Customer>().isNull(Customer::getDEndDate));
        for (Customer customer : list) {
            if(customer.getCCusRegCode() != null&& iTransPartnerService.list(new LambdaQueryWrapper<TransPartner>().eq(TransPartner::getCCusCode,customer.getCCusCode()).eq(TransPartner::getCCusName,customer.getCCusName()))==null){
                List<MDMPartner> mdmPartners = imdmPartnerService.list(new LambdaQueryWrapper<MDMPartner>().eq(MDMPartner::getScc, customer.getCCusRegCode()).eq(MDMPartner::getNameChs, customer.getCCusName()));
                if(mdmPartners.size()!=0){
                    MDMPartner mdmPartner =mdmPartners.get(0);
                    TransPartner transPartner = new TransPartner();
                    transPartner.setLId(mdmPartner.getId());
                    transPartner.setCode(mdmPartner.getCode());
                    transPartner.setNameChs(mdmPartner.getNameChs());
                    transPartner.setPartnerarea(mdmPartner.getPartnerarea());
                    transPartner.setScc(mdmPartner.getScc());
                    transPartner.setBelTable("0");
                    transPartner.setCCusCode(customer.getCCusCode());
                    transPartner.setCCusName(customer.getCCusName());
                    transPartner.setCDCCode(customer.getCDCCode());
                    transPartner.setCCusRegCode(customer.getCCusRegCode());
                    iTransPartnerService.saveOrUpdate(transPartner);
                }else {
                    reportCustomer(customer);
                }
            }else {
                reportCustomer(customer);
            }
        }
        List<Vendor> vendors = iVendorService.list(new LambdaQueryWrapper<Vendor>().isNull(Vendor::getDEndDate));
        for (Vendor vendor : vendors) {
            if(vendor.getCVenRegCode() != null && iTransPartnerService.list(new LambdaQueryWrapper<TransPartner>().eq(TransPartner::getCCusCode,vendor.getCVenCode()).eq(TransPartner::getCCusName,vendor.getCVenName()))==null){
                List<MDMPartner> mdmPartners = imdmPartnerService.list(new LambdaQueryWrapper<MDMPartner>().eq(MDMPartner::getScc, vendor.getCVenRegCode()).eq(MDMPartner::getNameChs, vendor.getCVenName()));
                if(mdmPartners.size()!=0){
                    MDMPartner mdmPartner =mdmPartners.get(0);
                    TransPartner transPartner = new TransPartner();
                    transPartner.setLId(mdmPartner.getId());
                    transPartner.setCode(mdmPartner.getCode());
                    transPartner.setNameChs(mdmPartner.getNameChs());
                    transPartner.setPartnerarea(mdmPartner.getPartnerarea());
                    transPartner.setScc(mdmPartner.getScc());
                    transPartner.setBelTable("1");
                    transPartner.setCCusCode(vendor.getCVenCode());
                    transPartner.setCCusName(vendor.getCVenName());
                    transPartner.setCDCCode(vendor.getCDCCode());
                    transPartner.setCCusRegCode(vendor.getCVenRegCode());
                    iTransPartnerService.saveOrUpdate(transPartner);
                }else {
                    reportVendor(vendor);
                }
            }else {
                reportVendor(vendor);
            }
        }
    }

    /**
     * 行政人员
     */
    public Boolean getBfemployeezjb() {
        Bfemployeezjb bfemployeezjb = new Bfemployeezjb();
        bfemployeezjb.setSecuritylevel("BFPUBLIC");
        bfemployeezjb.setSeclevel("0");
        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.in(Department::getCDepName, "综合管理部、规划运营部、改革办公室、人力资源部、财务部、党群工作部、纪检审计部、市场发展部".split("、"));
        List<Department> departmentList = departmentService.list(departmentLambdaQueryWrapper);
        for (Department department : departmentList) {
            List<Person> list = personService.list(new LambdaQueryWrapper<Person>().eq(Person::getCDepCode, department.getCDepCode()));
            for (Person person : list) {
                if (iBfemployeezjbService.getById(getPersonGuid(person.getCPersonCode()).getId()) == null) {
//                    注意！此处必须先执行getGUID
                    bfemployeezjb.setId(getPersonGuid(person.getCPersonCode()).getId());
                    int length = person.getCPersonCode().length();
                    bfemployeezjb.setCode(person.getCPersonCode().substring(length - 8, length));
                    bfemployeezjb.setDisplayname(person.getCPersonName());
                    bfemployeezjb.setNameChs(person.getCPersonName());
                    bfemployeezjb.setOrganization(getDepartment(person.getCDepCode()).getCDepGUID());
                    if (person.getDPInValidDate() != null) {
                        bfemployeezjb.setStateIsenabled("0");
                    } else {
                        bfemployeezjb.setStateIsenabled("1");
                    }
                    iBfemployeezjbService.save(bfemployeezjb);
                }
            }
        }
        return true;
    }

    /**
     * 核算人员
     */
    public void getBfaccountingemployeezjb() {
        Bfaccountingemployeezjb bfaccountingemployeezjb = new Bfaccountingemployeezjb();
        bfaccountingemployeezjb.setAccorgid(getMdmAccountOrg().getId());
        bfaccountingemployeezjb.setSecuritylevel("FI_PUBLIC");
        LambdaQueryWrapper<Department> departmentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        departmentLambdaQueryWrapper.eq(Department::getCDepName, "财务部");
        Department department = departmentService.getOne(departmentLambdaQueryWrapper);
        bfaccountingemployeezjb.setAccdeptid(department.getCDepGUID());
        List<Person> list = personService.list(new LambdaQueryWrapper<Person>().eq(Person::getCDepCode, department.getCDepCode()));
        for (Person person : list) {
            if (iBfaccountingemployeezjbService.getOne(new LambdaQueryWrapper<Bfaccountingemployeezjb>().eq(Bfaccountingemployeezjb::getAdminemployeeid, getPersonGuid(person.getCPersonCode()).getId())) == null) {
                bfaccountingemployeezjb.setId(UUID.randomUUID().toString());
                int length = person.getCPersonCode().length();
                bfaccountingemployeezjb.setAdminemployeeid(getPersonGuid(person.getCPersonCode()).getId());
                bfaccountingemployeezjb.setCode(person.getCPersonCode().substring(length - 8, length));
                bfaccountingemployeezjb.setNameChs(person.getCPersonName());
                if (person.getDPInValidDate() != null) {
                    bfaccountingemployeezjb.setIsdisable("1");
                } else {
                    bfaccountingemployeezjb.setIsdisable("0");
                }
                iBfaccountingemployeezjbService.save(bfaccountingemployeezjb);
            }
        }
    }

    public String getTreeInfoPath(Department department) {
        if (department.getIDepGrade() != 1) {
            Department parentDepartment = departmentService.getOne(new LambdaQueryWrapper<Department>().eq(Department::getCDepName, department.getCDepFullName().split("/")[department.getIDepGrade() - 2]));
            return getTreeInfoPath(parentDepartment) + StringUtils.leftPad(department.getIDepOrder().toString(), 4, '0');
        } else {
            return StringUtils.leftPad(department.getCDepCode(), 4, '0');
        }
    }

    public void getFiglaccdoctypezjb() {
        Figlaccdoctypezjb figlaccdoctypezjb = new Figlaccdoctypezjb();
        figlaccdoctypezjb.setAccorg(getMdmAccountOrg().getId());
        figlaccdoctypezjb.setAccset(getMdmLedger().getId());
        figlaccdoctypezjb.setCashflow("0");
        figlaccdoctypezjb.setBudgetflag("0");
        figlaccdoctypezjb.setFiflag("1");
        figlaccdoctypezjb.setCashiersignature("0");
        figlaccdoctypezjb.setYear("2023");
        figlaccdoctypezjb.setSecuritylevel("FI_PUBLIC");
        figlaccdoctypezjb.setOffbalancesheet("0");
        List<Dsign> list = dsignService.list();
        for (Dsign dsign : list) {
            if (iFiglaccdoctypezjbService.getOne(new LambdaQueryWrapper<Figlaccdoctypezjb>().eq(Figlaccdoctypezjb::getCode, StringUtils.leftPad(dsign.getIId().toString(), 2, '0'))) == null) {
                figlaccdoctypezjb.setId(UUID.randomUUID().toString());
                figlaccdoctypezjb.setCode(StringUtils.leftPad(dsign.getIId().toString(), 2, '0'));
                figlaccdoctypezjb.setVoucherword(dsign.getCsign());
                figlaccdoctypezjb.setNameChs(dsign.getCtext());
                figlaccdoctypezjb.setFiaccdocword(dsign.getCsign());
                iFiglaccdoctypezjbService.save(figlaccdoctypezjb);
            }
        }
    }

    public void getBfcustomitemzjb() {
        Bfcustomitemzjb bfcustomitemzjb = new Bfcustomitemzjb();
        bfcustomitemzjb.setAccountorg(getMdmAccountOrg().getId());
        bfcustomitemzjb.setAllowaddlower("1");
        bfcustomitemzjb.setIscompleted("0");
        bfcustomitemzjb.setSecuritylevel("FI_PUBLIC");
        bfcustomitemzjb.setIsdisabled("0");
        List<Bank> banks = iBankService.list();
        List<Fitemss98> fitemss98List = iFitemss98Service.list();
        for (int i = 0; i < fitemss98List.size(); i++) {
            String cValue = fitemss98List.get(i).getCitemname();
            if(iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, cValue))==null) {
                bfcustomitemzjb.setId(UUID.randomUUID().toString());
                bfcustomitemzjb.setCusitemcode("01"+StringUtils.leftPad(String.valueOf(i), 4, '0'));
                bfcustomitemzjb.setTreeinfoIsdetail("1");
                bfcustomitemzjb.setTreeinfoLayer("2");
                bfcustomitemzjb.setTreeinfoPath("0001"+StringUtils.leftPad(String.valueOf(i), 4, '0'));
                bfcustomitemzjb.setCusitemnameChs1(cValue);
                bfcustomitemzjb.setCusitemfullnameChs1("现金流项目-"+cValue);
                bfcustomitemzjb.setCusitemcategory(imdmAccountProTypeService.getOne(new LambdaQueryWrapper<MDMAccountProType>().eq(MDMAccountProType::getNameChs, "现金流项目")).getId());
                iBfcustomitemzjbService.save(bfcustomitemzjb);
            }
        }
        int count=fitemss98List.size();
        List<CustomerBank> customerBanks = iCustomerBankService.list();
        for (int i = 0; i < customerBanks.size(); i++) {
            String cValue = customerBanks.get(i).getCBranch();
            if(iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, cValue))==null) {
                bfcustomitemzjb.setId(UUID.randomUUID().toString());
                bfcustomitemzjb.setCusitemcode("02"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setTreeinfoIsdetail("1");
                bfcustomitemzjb.setTreeinfoLayer("2");
                bfcustomitemzjb.setTreeinfoPath("0002"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setCusitemnameChs1(cValue);
                bfcustomitemzjb.setCusitemfullnameChs1("银行账户-"+cValue);
                bfcustomitemzjb.setCusitemcategory(imdmAccountProTypeService.getOne(new LambdaQueryWrapper<MDMAccountProType>().eq(MDMAccountProType::getNameChs, "银行账户")).getId());
                iBfcustomitemzjbService.save(bfcustomitemzjb);
            }
        }
        count+=customerBanks.size();
        List<Fitemss03> fitemss03List = iFitemss03Service.list();
        for (int i = 0; i < fitemss03List.size(); i++) {
            String cValue = fitemss03List.get(i).getCitemname();
            if(iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, cValue))==null) {
                bfcustomitemzjb.setId(UUID.randomUUID().toString());
                bfcustomitemzjb.setCusitemcode("03"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setTreeinfoIsdetail("1");
                bfcustomitemzjb.setTreeinfoLayer("2");
                bfcustomitemzjb.setTreeinfoPath("0003"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setCusitemnameChs1(cValue);
                bfcustomitemzjb.setCusitemfullnameChs1("业务类型-"+cValue);
                bfcustomitemzjb.setCusitemcategory(imdmAccountProTypeService.getOne(new LambdaQueryWrapper<MDMAccountProType>().eq(MDMAccountProType::getNameChs, "业务类型")).getId());
                iBfcustomitemzjbService.save(bfcustomitemzjb);
            }
        }
        count+=fitemss03List.size();
        for (int i = 0; i < banks.size(); i++) {
            String cValue = banks.get(i).getCBName();
            if(iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, cValue))==null){
                bfcustomitemzjb.setId(UUID.randomUUID().toString());
                bfcustomitemzjb.setCusitemcode("05"+StringUtils.leftPad(String.valueOf(i), 4, '0'));
                bfcustomitemzjb.setTreeinfoIsdetail("1");
                bfcustomitemzjb.setTreeinfoLayer("2");
                bfcustomitemzjb.setTreeinfoPath("0005"+StringUtils.leftPad(String.valueOf(i), 4, '0'));
                bfcustomitemzjb.setCusitemnameChs1(cValue);
                bfcustomitemzjb.setCusitemfullnameChs1("成本中心段-"+cValue);
                bfcustomitemzjb.setCusitemcategory(imdmAccountProTypeService.getOne(new LambdaQueryWrapper<MDMAccountProType>().eq(MDMAccountProType::getNameChs, "成本中心段")).getId());
                iBfcustomitemzjbService.save(bfcustomitemzjb);
            }
        }
        count+=banks.size();
        List<Inventory> inventories = iInventoryService.list();
        for (int i = 0; i < inventories.size(); i++) {
            String cValue = inventories.get(i).getCInvName();
            if(iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, cValue))==null) {
                bfcustomitemzjb.setId(UUID.randomUUID().toString());
                bfcustomitemzjb.setCusitemcode("06"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setTreeinfoIsdetail("1");
                bfcustomitemzjb.setTreeinfoLayer("2");
                bfcustomitemzjb.setTreeinfoPath("0006"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setCusitemnameChs1(cValue);
                bfcustomitemzjb.setCusitemfullnameChs1("产品/服务段-"+cValue);
                bfcustomitemzjb.setCusitemcategory(imdmAccountProTypeService.getOne(new LambdaQueryWrapper<MDMAccountProType>().eq(MDMAccountProType::getNameChs, "产品/服务段")).getId());
                iBfcustomitemzjbService.save(bfcustomitemzjb);
            }
        }
        count+=inventories.size();
        List<Fitemss00> fitemss00List = iFitemss00Service.list();
        for (int i = 0; i < fitemss00List.size(); i++) {
            String cValue = fitemss00List.get(i).getCitemname();
            if(iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, cValue))==null) {
                bfcustomitemzjb.setId(UUID.randomUUID().toString());
                bfcustomitemzjb.setCusitemcode("07"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setTreeinfoIsdetail("1");
                bfcustomitemzjb.setTreeinfoLayer("2");
                bfcustomitemzjb.setTreeinfoPath("0007"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setCusitemnameChs1(cValue);
                bfcustomitemzjb.setCusitemfullnameChs1("专项项目段-"+cValue);
                bfcustomitemzjb.setCusitemcategory(imdmAccountProTypeService.getOne(new LambdaQueryWrapper<MDMAccountProType>().eq(MDMAccountProType::getNameChs, "专项项目段")).getId());
                iBfcustomitemzjbService.save(bfcustomitemzjb);
            }
        }
        count+=fitemss00List.size();
        List<Fitemss02> fitemss02List = iFitemss02Service.list();
        for (int i = 0; i < fitemss02List.size(); i++) {
            String cValue = fitemss02List.get(i).getCitemname();
            if(iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, cValue))==null) {
                bfcustomitemzjb.setId(UUID.randomUUID().toString());
                bfcustomitemzjb.setCusitemcode("08"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setTreeinfoIsdetail("1");
                bfcustomitemzjb.setTreeinfoLayer("2");
                bfcustomitemzjb.setTreeinfoPath("0008"+StringUtils.leftPad(String.valueOf(count+i), 4, '0'));
                bfcustomitemzjb.setCusitemnameChs1(cValue);
                bfcustomitemzjb.setCusitemfullnameChs1("项目段-"+cValue);
                bfcustomitemzjb.setCusitemcategory(imdmAccountProTypeService.getOne(new LambdaQueryWrapper<MDMAccountProType>().eq(MDMAccountProType::getNameChs, "项目段")).getId());
                iBfcustomitemzjbService.save(bfcustomitemzjb);
            }
        }
    }

    @GetMapping("/autoTransfer/{ids}")
    public JsonResult<String> autoTransfer(@PathVariable List<String> ids) {
        List<AssociatedCertificate> glAccvouches = associatedCertificateService.list(new LambdaQueryWrapper<AssociatedCertificate>().in(AssociatedCertificate::getIId,ids));

        for (AssociatedCertificate associatedCertificate : glAccvouches) {
            associatedCertificate.setIsConvert("1");
            associatedCertificate.setConvertTime(new Date());
            associatedCertificate.setConverReason(null);
            Dsign dsign = dsignService.getOne(new LambdaQueryWrapper<Dsign>().eq(Dsign::getIsignseq, associatedCertificate.getIsignseq()));
            Figlaccdoctypezjb one = iFiglaccdoctypezjbService.getOne(new LambdaQueryWrapper<Figlaccdoctypezjb>().eq(Figlaccdoctypezjb::getCode, StringUtils.leftPad(dsign.getIId().toString(), 2, '0')));
//            凭证头
            Figlaccountingdocumentzjb figlaccountingdocumentzjb = new Figlaccountingdocumentzjb();
            figlaccountingdocumentzjb.setAccdoccode(one.getFiaccdocword() + StringUtils.leftPad(associatedCertificate.getInoId(), 4, '0'));
            SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMdd");
            String format = sdf1.format(associatedCertificate.getDbillDate());
            figlaccountingdocumentzjb.setAccdocdate(format);
//            特别注意！！！该处是取凭证类型ID
            figlaccountingdocumentzjb.setAccdoctypeid(one.getId());
//            figlaccountingdocumentzjb.setAccmanagerid();
//            figlaccountingdocumentzjb.setAccmanagername();
            figlaccountingdocumentzjb.setAccorgid(getMdmAccountOrg().getId());
//            会计期间编号
            figlaccountingdocumentzjb.setAccperiodcode(StringUtils.leftPad(associatedCertificate.getIperiod().toString(), 2, '0'));
            LambdaQueryWrapper<MDMAccountPeriod> periodQueryWrapper = new LambdaQueryWrapper<>();
            if (associatedCertificate.getIperiod() > 12 || associatedCertificate.getIperiod() == 0) {
                associatedCertificate.setIsConvert("0");
                associatedCertificate.setConverReason("会记期间为" + associatedCertificate.getIperiod() + "，无法自动转换");
                associatedCertificateService.updateById(associatedCertificate);
                continue;
//                return new JsonResult<>(false, associatedCertificate.getCdigest() + "的会记期间为" + associatedCertificate.getIperiod() + "，无法自动转换");
            }
            periodQueryWrapper.eq(MDMAccountPeriod::getTimenumber, StringUtils.leftPad(associatedCertificate.getIperiod().toString(), 2, '0'));
            periodQueryWrapper.eq(MDMAccountPeriod::getParentid, "3a2476d9-616f-499b-8ce8-8c2f448c9fb2");
            MDMAccountPeriod period = imdmAccountPeriodService.getOne(periodQueryWrapper);
            figlaccountingdocumentzjb.setAccperiodid(period.getId());
            figlaccountingdocumentzjb.setAmount(associatedCertificate.getMd().toString());
//            figlaccountingdocumentzjb.setBudgetabstract();
//            figlaccountingdocumentzjb.setBudgetaccdoccode();
            figlaccountingdocumentzjb.setBudgetamount("0");
            figlaccountingdocumentzjb.setDwversion("0");
            figlaccountingdocumentzjb.setFiaccdoccode(one.getFiaccdocword() + StringUtils.leftPad(associatedCertificate.getInoId(), 4, '0'));
            figlaccountingdocumentzjb.setIsapproved("0");
            figlaccountingdocumentzjb.setIsaudit("0");
            figlaccountingdocumentzjb.setIsbook("0");
            figlaccountingdocumentzjb.setLedger(getMdmLedger().getId());
//            特别注意！！！制单人CODE,关联核算人员code
            figlaccountingdocumentzjb.setMakername(associatedCertificate.getCbill());
            Person person = personService.getOne(new LambdaQueryWrapper<Person>().eq(Person::getCPersonName, associatedCertificate.getCbill()));
            if (person != null) {
                int length = person.getCPersonCode().length();
                if(length>8){
                    figlaccountingdocumentzjb.setMakercode(person.getCPersonCode().substring(length-8,length));
                }else {
                    figlaccountingdocumentzjb.setMakercode(StringUtils.leftPad(person.getCPersonCode(), 8, '0'));
                }
            }else {
                figlaccountingdocumentzjb.setMakercode("12345678");
            }
            figlaccountingdocumentzjb.setNumberofattch("0");
            figlaccountingdocumentzjb.setNumberofnote("0");
            figlaccountingdocumentzjb.setReadonlyflag("0");
            figlaccountingdocumentzjb.setSecretlevelid("FI_PUBLIC");
            figlaccountingdocumentzjb.setSecretlevelnum("0");
            figlaccountingdocumentzjb.setYear("2023");
            if (associatedCertificate.getCdigest().length() > 1024) {
                figlaccountingdocumentzjb.setAbstract1(associatedCertificate.getCdigest().substring(0, 1024));
            } else {
                figlaccountingdocumentzjb.setAbstract1(associatedCertificate.getCdigest());
            }
            figlaccountingdocumentzjb.setExternaldoc(associatedCertificate.getIId().toString());
            String figlaccountingdocumentzjbId = associatedCertificate.getId();
            figlaccountingdocumentzjb.setId(figlaccountingdocumentzjbId);
            figlaccountingdocumentzjbService.saveOrUpdate(figlaccountingdocumentzjb);
            associatedCertificate.setFiglaccountingdocumentzjbId(figlaccountingdocumentzjbId);

//            凭证编号文件
            Figlaccdoccodezjb figlaccdoccodezjb = new Figlaccdoccodezjb();
            String figlaccdoccodezjbId = UUID.randomUUID().toString();
            figlaccdoccodezjb.setId(figlaccdoccodezjbId);
            figlaccdoccodezjb.setAccdoccode(associatedCertificate.getInoId());
            figlaccdoccodezjb.setAccdocdate(format);
            figlaccdoccodezjb.setAccdoctypeword(one.getFiaccdocword());
            figlaccdoccodezjb.setAccorgid(getMdmAccountOrg().getId());
            figlaccdoccodezjb.setAccperiod(period.getId());
            figlaccdoccodezjb.setCodetype("M");
            figlaccdoccodezjb.setLedger(getMdmLedger().getId());
            figlaccdoccodezjb.setYear("2023");
            log.info("figlaccdoccodezjb", figlaccdoccodezjb);
            figlaccdoccodezjbService.save(figlaccdoccodezjb);
            associatedCertificate.setFiglaccdoccodezjbId(figlaccdoccodezjbId);

//            凭证分录文件
            LambdaQueryWrapper<GlAccvouch> entryQueryWrapper = new LambdaQueryWrapper<>();
            entryQueryWrapper.eq(GlAccvouch::getInoId, associatedCertificate.getInoId());
            entryQueryWrapper.eq(GlAccvouch::getIyear, associatedCertificate.getIyear());
            entryQueryWrapper.eq(GlAccvouch::getIperiod, associatedCertificate.getIperiod());
            List<GlAccvouch> entries = glAccvouchService.list(entryQueryWrapper);
            Figlaccdocentryzjb figlaccdocentryzjb = new Figlaccdocentryzjb();
            figlaccdocentryzjb.setAccorgid(getMdmAccountOrg().getId());
            associatedCertificate.setFiglaccdocentryzjbId(null);
            for (GlAccvouch entry : entries) {
                figlaccdocentryzjb.setAccdocid(figlaccountingdocumentzjbId);
                String figlaccdocentryzjbId = UUID.randomUUID().toString();
                figlaccdocentryzjb.setId(figlaccdocentryzjbId);
                figlaccdocentryzjb.setAccentrycode(StringUtils.leftPad(entry.getInid().toString(), 5, '0'));
                LambdaQueryWrapper<MDMSubject> accentryQueryWrapper = new LambdaQueryWrapper<>();
                if(entry.getCcode().startsWith("2204")){
                    entry.setCcode(entry.getCcode().replace("2204","2205"));
                }
                accentryQueryWrapper.eq(MDMSubject::getCode, entry.getCcode());
                accentryQueryWrapper.eq(MDMSubject::getSubjectsystemid, "80c225cb-6d28-4833-8cca-fb142c3c7d1f");
                MDMSubject mdmSubject = imdmSubjectService.getOne(accentryQueryWrapper);
                figlaccdocentryzjb.setAcctitlecode(entry.getCcode());
                if (mdmSubject != null) {
                    figlaccdocentryzjb.setAcctitleid(mdmSubject.getId());
                } else {
//                    特别注意，正式环境要改
//                    figlaccdocentryzjb.setAcctitleid(UUID.randomUUID().toString());
                    associatedCertificate.setIsConvert("0");
                    associatedCertificate.setConverReason("在系统中找不到编码为" + entry.getCcode() + "的会计科目，请在浪潮录入相应的会计科目并导入本系统再做转换");
                    log.info("转换后的凭证转换表", associatedCertificate);
                    associatedCertificateService.updateById(associatedCertificate);
                    if (glAccvouches.size() > 1) {
                        continue;
                    } else {
                        return new JsonResult<>(false, "在系统中找不到编码为" + entry.getCcode() + "对应，请在浪潮录入相应的会计科目并导入本系统");
                    }
                }
                figlaccdocentryzjb.setAcctgtype("0");
                if (entry.getMc() != 0) {
                    figlaccdocentryzjb.setAmount(entry.getMc().toString());
                    figlaccdocentryzjb.setForeigncurrency(entry.getMc().toString());
                    figlaccdocentryzjb.setLendingdirection("2");
                    if (entry.getNcS() != null) {
                        figlaccdocentryzjb.setQuantity(entry.getNcS().toString());
                    } else {
                        figlaccdocentryzjb.setQuantity("0");
                    }
                } else {
                    figlaccdocentryzjb.setAmount(entry.getMd().toString());
                    figlaccdocentryzjb.setForeigncurrency(entry.getMd().toString());
                    figlaccdocentryzjb.setLendingdirection("1");
                    if (entry.getNdS() != null) {
                        figlaccdocentryzjb.setQuantity(entry.getNdS().toString());
                    } else {
                        figlaccdocentryzjb.setQuantity("0");
                    }
                }
                if (entry.getDtDate() != null) {
                    String entryDate = sdf1.format(entry.getDtDate());
                    figlaccdocentryzjb.setBizdate(entryDate);
                } else {
                    String entryDate = sdf1.format(entry.getDbillDate());
                    figlaccdocentryzjb.setBizdate(entryDate);
                }
//               人民币Id
                figlaccdocentryzjb.setCurrencyid(getMdmCurrency().getId());
                figlaccdocentryzjb.setExchangerate("1");
                figlaccdocentryzjb.setLedger(getMdmLedger().getId());
                if (entry.getCcodeEqual() != null) {
//                    对方科目原系统按逗号隔开
                    LambdaQueryWrapper<MDMSubject> subjectQueryWrapper = new LambdaQueryWrapper<>();
                    String code = entry.getCcodeEqual().split(",")[entry.getCcodeEqual().split(",").length - 1];
                    if(code.startsWith("2204")){
                        code=code.replace("2204","2205");
                    }
                    subjectQueryWrapper.eq(MDMSubject::getCode, code);
                    subjectQueryWrapper.eq(MDMSubject::getSubjectsystemid, "80c225cb-6d28-4833-8cca-fb142c3c7d1f");
                    MDMSubject mdmSubjectOne = imdmSubjectService.getOne(subjectQueryWrapper);
                    figlaccdocentryzjb.setAcctitlecode(entry.getCcode());
                    if (mdmSubjectOne != null) {
                        figlaccdocentryzjb.setOppositeacctitle(mdmSubjectOne.getId());
                    } else {
//                    特别注意，正式环境要改
//                        figlaccdocentryzjb.setOppositeacctitle(UUID.randomUUID().toString());
                        associatedCertificate.setIsConvert("0");
                        associatedCertificate.setConverReason("在系统中找不到编码为" + entry.getCcode() + "的会计科目，请在浪潮录入相应的会计科目并导入本系统");
                        log.info("转换后的凭证转换表", associatedCertificate);
                        associatedCertificateService.updateById(associatedCertificate);
                        if (glAccvouches.size() > 1) {
                            continue;
                        } else {
                            return new JsonResult<>(false, "在系统中找不到编码为" + entry.getCcode() + "对应，请在浪潮录入相应的会计科目并导入本系统");
                        }
                    }
                    figlaccdocentryzjb.setOppositeacctitleflag("1");
                } else {
                    figlaccdocentryzjb.setOppositeacctitleflag("0");
                }
                figlaccdocentryzjb.setReadonly("0");
                if (entry.getCsettle() != null) {
                    figlaccdocentryzjb.setSettlementnumber(entry.getCsettle());
                    LambdaQueryWrapper<SettleStyle> settleQueryWrapper = new LambdaQueryWrapper<>();
                    settleQueryWrapper.eq(SettleStyle::getCSSCode, entry.getCcode());
                    SettleStyle settleStyle = settleStyleService.getOne(settleQueryWrapper);
                    if (settleStyle != null) {
                        figlaccdocentryzjb.setSettlement(settleStyle.getCSSName());
                    }
                }
                figlaccdocentryzjb.setUnitprice(entry.getIprice().toString());
                figlaccdocentryzjb.setYear("2023");
                if (entry.getCdigest().length() > 1024) {
                    figlaccdocentryzjb.setAbstract1(entry.getCdigest().substring(0, 1024));
                } else {
                    figlaccdocentryzjb.setAbstract1(entry.getCdigest());
                }
                log.info("凭证分录文件", figlaccdocentryzjb);
                figlaccdocentryzjbService.save(figlaccdocentryzjb);
                if (associatedCertificate.getFiglaccdocentryzjbId() != null) {
                    associatedCertificate.setFiglaccdocentryzjbId(associatedCertificate.getFiglaccdocentryzjbId() + ";" + figlaccdocentryzjbId);
                } else {
                    associatedCertificate.setFiglaccdocentryzjbId(figlaccdocentryzjbId);
                }
//                凭证辅助：5位流水号 记录值
                int assistId = 0;
                Figlaccdocassistancezjb figlaccdocassistancezjb = new Figlaccdocassistancezjb();
                figlaccdocassistancezjb.setAmount("0");
                figlaccdocassistancezjb.setForeigncurrencyid(getMdmCurrency().getId());
                figlaccdocassistancezjb.setAccdocentryid(figlaccdocentryzjbId);
                figlaccdocassistancezjb.setAccdocid(figlaccountingdocumentzjbId);
                figlaccdocassistancezjb.setAccorgid(getMdmAccountOrg().getId());
                figlaccdocassistancezjb.setAcctitlecode(entry.getCcode());
                if (mdmSubject != null) {
                    figlaccdocassistancezjb.setAcctitleid(mdmSubject.getId());
                } else {
//                    特别注意！！！正式环境需要改
//                    figlaccdocassistancezjb.setAcctitleid(UUID.randomUUID().toString());
                    associatedCertificate.setIsConvert("0");
                    associatedCertificate.setConverReason("在系统中找不到编码为" + entry.getCcode() + "的会计科目，请在浪潮录入相应的会计科目并导入本系统");
                    log.info("转换后的凭证转换表", associatedCertificate);
                    associatedCertificateService.updateById(associatedCertificate);
                    if (glAccvouches.size() > 1) {
                        continue;
                    } else {
                        return new JsonResult<>(false, "在系统中找不到编码为" + entry.getCcode() + "的会计科目，请在浪潮录入相应的会计科目并导入本系统");
                    }
                }
                figlaccdocassistancezjb.setAcctgtype("0");
                if (entry.getMc() != 0) {
                    figlaccdocassistancezjb.setAmount(entry.getMc().toString());
                    figlaccdocassistancezjb.setForeigncurrency(entry.getMc().toString());
                    figlaccdocassistancezjb.setLendingdirection("2");
                    if (entry.getNcS() != null) {
                        figlaccdocentryzjb.setQuantity(entry.getNcS().toString());
                        figlaccdocassistancezjb.setQuantity(entry.getNcS().toString());
                    } else {
                        figlaccdocentryzjb.setQuantity("0");
                        figlaccdocassistancezjb.setQuantity("0");
                    }
                } else {
                    figlaccdocassistancezjb.setAmount(entry.getMd().toString());
                    figlaccdocassistancezjb.setForeigncurrency(entry.getMd().toString());
                    figlaccdocassistancezjb.setLendingdirection("1");
                    if (entry.getNdS() != null) {
                        figlaccdocentryzjb.setQuantity(entry.getNdS().toString());
                        figlaccdocassistancezjb.setQuantity(entry.getNcS().toString());
                    } else {
                        figlaccdocentryzjb.setQuantity("0");
                        figlaccdocassistancezjb.setQuantity("0");
                    }
                }
                if (entry.getCnId() != null) {
                    figlaccdocassistancezjb.setBillnumber(entry.getCnId());
                }
//                凭证辅助的业务号
//                figlaccdocassistancezjb.setBizcode();
                if (entry.getDtDate() != null) {
                    String entryDateOne = sdf1.format(entry.getDtDate());
                    figlaccdocentryzjb.setBizdate(entryDateOne);
                    figlaccdocassistancezjb.setBizdate(entryDateOne);
                } else {
                    String entryDateOne = sdf1.format(entry.getDbillDate());
                    figlaccdocentryzjb.setBizdate(entryDateOne);
                    figlaccdocassistancezjb.setBizdate(entryDateOne);
                }
//                关联核算部门id
//                figlaccdocassistancezjb.setDeptid();
                figlaccdocassistancezjb.setDeptversion("0");
                figlaccdocassistancezjb.setExchangerate("1");

                figlaccdocassistancezjb.setLedger(getMdmLedger().getId());
//                凭证辅助的往来单位ID
//                figlaccdocassistancezjb.setRelatedorgid();
                figlaccdocassistancezjb.setRelatedorgversion("0");
                figlaccdocassistancezjb.setSj01("0");
                figlaccdocassistancezjb.setSj02("0");
                figlaccdocassistancezjb.setSj03("0");
                figlaccdocassistancezjb.setSj04("0");
                figlaccdocassistancezjb.setSj05("0");
                if (entry.getCsettle() != null) {
                    figlaccdocassistancezjb.setSettlementnumber(entry.getCsettle());
                    LambdaQueryWrapper<SettleStyle> settleQueryWrapper = new LambdaQueryWrapper<>();
                    settleQueryWrapper.eq(SettleStyle::getCSSCode, entry.getCcode());
                    SettleStyle settleStyle = settleStyleService.getOne(settleQueryWrapper);
                    if (settleStyle != null) {
                        figlaccdocassistancezjb.setSettlement(settleStyle.getCSSName());
                    }
                }
//                凭证的结算日期
//                figlaccdocassistancezjb.setSettlementdate();
//                凭证的集团核算项目类别
//                figlaccdocassistancezjb.setSpecateid02();
//                figlaccdocassistancezjb.setSpecateid03();
//                figlaccdocassistancezjb.setSpecateid04();
//                figlaccdocassistancezjb.setSpecateid05();
//                figlaccdocassistancezjb.setSpecateid06();
//                figlaccdocassistancezjb.setSpecateid07();
//                figlaccdocassistancezjb.setSpecateid08();
//                figlaccdocassistancezjb.setSpecateid09();
//                figlaccdocassistancezjb.setSpecateid10();
                figlaccdocassistancezjb.setUnitprice(entry.getIprice().toString());
                figlaccdocassistancezjb.setYear("2023");
                associatedCertificate.setFiglaccdocassistancezjbId(null);
                if (entry.getCdeptId() != null) {
                    LambdaQueryWrapper<Department> departmentLambdaQueryWrapperOne = new LambdaQueryWrapper<>();
                    departmentLambdaQueryWrapperOne.eq(Department::getCDepCode, entry.getCdeptId());
                    figlaccdocassistancezjb.setDeptid(departmentService.getOne(departmentLambdaQueryWrapperOne).getCDepGUID());
                    String figlaccdocassistancezjbId = UUID.randomUUID().toString();
                    figlaccdocassistancezjb.setId(figlaccdocassistancezjbId);
                    figlaccdocassistancezjb.setApplication1("部门核算");
                    assistId++;
                    figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                    if (entry.getCpersonId() != null) {
                        Bfaccountingemployeezjb bfaccountingemployeezjb = iBfaccountingemployeezjbService.getOne(new LambdaQueryWrapper<Bfaccountingemployeezjb>().eq(Bfaccountingemployeezjb::getAdminemployeeid, getPersonGuid(entry.getCpersonId()).getId()));
                        if(bfaccountingemployeezjb!=null){
                            figlaccdocassistancezjb.setAccemployeeid(bfaccountingemployeezjb.getId());
                        }
                        figlaccdocassistancezjb.setApplication1("个人往来");
                    }
                    log.info("凭证辅助文件", figlaccdocassistancezjb);
                    figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                    if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                        associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbId);
                    } else {
                        associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbId);
                    }
                }
                if (entry.getCcusId() != null) {
                    Customer customer = customerService.getOne(new LambdaQueryWrapper<Customer>().eq(Customer::getCCusCode, entry.getCcusId()));
                    Bfpartnerzjb bfpartnerzjb = bfpartnerzjbService.getOne(new LambdaQueryWrapper<Bfpartnerzjb>().eq(Bfpartnerzjb::getNameChs, customer.getCCusName()));
                    figlaccdocassistancezjb.setApplication1("往来单位");
                    if(bfpartnerzjb==null){
                        TransPartner transPartner = iTransPartnerService.getOne(new LambdaQueryWrapper<TransPartner>().eq(TransPartner::getCCusCode, customer.getCCusCode()).eq(TransPartner::getCCusName, customer.getCCusName()));
                        figlaccdocassistancezjb.setRelatedorgid(transPartner.getLId());
                    }else {
                        figlaccdocassistancezjb.setRelatedorgid(bfpartnerzjb.getId());
                    }
                    String figlaccdocassistancezjbId = UUID.randomUUID().toString();
                    figlaccdocassistancezjb.setId(figlaccdocassistancezjbId);
                    assistId++;
                    figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                    log.info("凭证辅助文件", figlaccdocassistancezjb);
                    figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                    if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                        associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbId);
                    } else {
                        associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbId);
                    }
                }
                if (entry.getCsupId() != null) {
                    Vendor vendor = iVendorService.getOne(new LambdaQueryWrapper<Vendor>().eq(Vendor::getCVenCode, entry.getCsupId()));
                    Bfpartnerzjb bfpartnerzjb = bfpartnerzjbService.getOne(new LambdaQueryWrapper<Bfpartnerzjb>().eq(Bfpartnerzjb::getNameChs, vendor.getCVenName()));
                    figlaccdocassistancezjb.setApplication1("往来单位");
                    if(bfpartnerzjb==null){
                        TransPartner transPartner = iTransPartnerService.getOne(new LambdaQueryWrapper<TransPartner>().eq(TransPartner::getCCusCode, vendor.getCVenCode()).eq(TransPartner::getCCusName, vendor.getCVenName()));
                        figlaccdocassistancezjb.setRelatedorgid(transPartner.getLId());
                    }else {
                        figlaccdocassistancezjb.setRelatedorgid(bfpartnerzjb.getId());
                    }
                    String figlaccdocassistancezjbId = UUID.randomUUID().toString();
                    figlaccdocassistancezjb.setId(figlaccdocassistancezjbId);
                    assistId++;
                    figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                    log.info("凭证辅助文件", figlaccdocassistancezjb);
                    figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                    if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                        associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbId);
                    } else {
                        associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbId);
                    }
                }
                if (entry.getCitemId() != null) {
                    switch (entry.getCitemClass()){
                        case "98":
                            Fitemss98 fitemss98 = iFitemss98Service.getOne(new LambdaQueryWrapper<Fitemss98>().eq(Fitemss98::getCitemcode,entry.getCitemId()));
                            Bfcustomitemzjb bfcustomitemzjb = iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, fitemss98.getCitemname()));
                            figlaccdocassistancezjb.setApplication1("现金流项目");
                            figlaccdocassistancezjb.setSpecateid01(bfcustomitemzjb.getId());
                            String figlaccdocassistancezjbId = UUID.randomUUID().toString();
                            figlaccdocassistancezjb.setId(figlaccdocassistancezjbId);
                            assistId++;
                            figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                            log.info("凭证辅助文件", figlaccdocassistancezjb);
                            figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                            if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                                associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbId);
                            } else {
                                associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbId);
                            }
                            break;
                        case "00":
                            Fitemss00 fitemss00 = iFitemss00Service.getOne(new LambdaQueryWrapper<Fitemss00>().eq(Fitemss00::getCitemcode,entry.getCitemId()));
                            Bfcustomitemzjb bfcustomitemzjb1 = iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, fitemss00.getCitemname()));
                            figlaccdocassistancezjb.setApplication1("专项项目段");
                            figlaccdocassistancezjb.setSpecateid07(bfcustomitemzjb1.getId());
                            String figlaccdocassistancezjbId00 = UUID.randomUUID().toString();
                            figlaccdocassistancezjb.setId(figlaccdocassistancezjbId00);
                            assistId++;
                            figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                            log.info("凭证辅助文件", figlaccdocassistancezjb);
                            figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                            if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                                associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbId00);
                            } else {
                                associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbId00);
                            }
                            break;
                        case "02":
                            Fitemss02 fitemss02 = iFitemss02Service.getOne(new LambdaQueryWrapper<Fitemss02>().eq(Fitemss02::getCitemcode,entry.getCitemId()));
                            Bfcustomitemzjb bfcustomitemzjb02 = iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, fitemss02.getCitemname()));
                            figlaccdocassistancezjb.setApplication1("项目段");
                            figlaccdocassistancezjb.setSpecateid08(bfcustomitemzjb02.getId());
                            String figlaccdocassistancezjbId02 = UUID.randomUUID().toString();
                            figlaccdocassistancezjb.setId(figlaccdocassistancezjbId02);
                            assistId++;
                            figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                            log.info("凭证辅助文件", figlaccdocassistancezjb);
                            figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                            if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                                associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbId02);
                            } else {
                                associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbId02);
                            }
                            break;
                        case "03":
                            Fitemss03 fitemss03 = iFitemss03Service.getOne(new LambdaQueryWrapper<Fitemss03>().eq(Fitemss03::getCitemcode,entry.getCitemId()));
                            Bfcustomitemzjb bfcustomitemzjb03 = iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, fitemss03.getCitemname()));
                            figlaccdocassistancezjb.setApplication1("业务类型");
                            figlaccdocassistancezjb.setSpecateid03(bfcustomitemzjb03.getId());
                            String figlaccdocassistancezjbId03 = UUID.randomUUID().toString();
                            figlaccdocassistancezjb.setId(figlaccdocassistancezjbId03);
                            assistId++;
                            figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                            log.info("凭证辅助文件", figlaccdocassistancezjb);
                            figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                            if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                                associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbId03);
                            } else {
                                associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbId03);
                            }
                            break;
                        case "ZF":
                            FitemssZF fitemssZF = iFitemssZFService.getOne(new LambdaQueryWrapper<FitemssZF>().eq(FitemssZF::getCitemcode,entry.getCitemId()));
                            Bfcustomitemzjb bfcustomitemzjbZF = iBfcustomitemzjbService.getOne(new LambdaQueryWrapper<Bfcustomitemzjb>().eq(Bfcustomitemzjb::getCusitemnameChs1, fitemssZF.getCitemname()));
                            figlaccdocassistancezjb.setApplication1("自定义辅助核算");
                            figlaccdocassistancezjb.setSpecateid01(bfcustomitemzjbZF.getId());
                            String figlaccdocassistancezjbIdZF = UUID.randomUUID().toString();
                            figlaccdocassistancezjb.setId(figlaccdocassistancezjbIdZF);
                            assistId++;
                            figlaccdocassistancezjb.setAccasscode(StringUtils.leftPad(String.valueOf(assistId), 5, '0'));
                            log.info("凭证辅助文件", figlaccdocassistancezjb);
                            figlaccdocassistancezjbService.save(figlaccdocassistancezjb);
                            if (associatedCertificate.getFiglaccdocassistancezjbId() != null) {
                                associatedCertificate.setFiglaccdocassistancezjbId(associatedCertificate.getFiglaccdocassistancezjbId() + ";" + figlaccdocassistancezjbIdZF);
                            } else {
                                associatedCertificate.setFiglaccdocassistancezjbId(figlaccdocassistancezjbIdZF);
                            }
                            break;
                    }
                }
            }
            associatedCertificate.setConverReason(null);
            log.info("转换后的凭证转换表", associatedCertificate);
            associatedCertificateService.updateById(associatedCertificate);
        }
        return new JsonResult<>(true, "成功");
    }

}
